﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpResumeWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_ResumeModel model = new EmpbasicService.Emp_ResumeModel();
        private EmpbasicService.Emp_ResumeModel oldmodel = new EmpbasicService.Emp_ResumeModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpResumeWindow(int empID, int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增简历信息";
                model = new EmpbasicService.Emp_ResumeModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑简历信息";
                Client.GetEmp_ResumeCompleted += new EventHandler<EmpbasicService.GetEmp_ResumeCompletedEventArgs>(Client_GetEmp_ResumeCompleted);
                Client.GetEmp_ResumeAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_ResumeCompleted(object sender, EmpbasicService.GetEmp_ResumeCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_ResumeModel
                {
                    BeginDate = model.BeginDate,
                    EndDate = model.EndDate,
                    Unit = model.Unit,
                    Duty = model.Duty,
                    Witness = model.Witness,
                    IsPeriod = model.IsPeriod
                };
            }
            this.DataContext = model;
            if(model.IsPeriod == true)
            {
                check.IsChecked = true;
            }
            else
            {
                check.IsChecked = false;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (check.IsChecked == true)
            {
                model.IsPeriod = true;
            }
            else
            {
                model.IsPeriod = false;
            }
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;
                
                Client.AddEmp_ResumeCompleted += new EventHandler<EmpbasicService.AddEmp_ResumeCompletedEventArgs>(Client_AddEmp_ResumeCompleted);
                Client.AddEmp_ResumeAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.BeginDate = Convert.ToDateTime(begin.Text); else
                    model.BeginDate = null;
                if (!string.IsNullOrEmpty(end.Text)) model.EndDate = Convert.ToDateTime(end.Text); else
                    model.EndDate = null;
                Client.UpdateEmp_ResumeModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_ResumeModelCompletedEventArgs>(Client_UpdateEmp_ResumeModelCompleted);
                Client.UpdateEmp_ResumeModelAsync(model);
            }
        }

        void Client_UpdateEmp_ResumeModelCompleted(object sender, EmpbasicService.UpdateEmp_ResumeModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_ResumeCompleted(object sender, EmpbasicService.AddEmp_ResumeCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("简历子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.BeginDate != model.BeginDate)
                    insertsys.Insert("简历子集", oldmodel.BeginDate == null ? null : Convert.ToDateTime(oldmodel.BeginDate).ToShortDateString(),
                        model.BeginDate == null ? null : Convert.ToDateTime(model.BeginDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "开始日期");
                if (oldmodel.EndDate != model.EndDate)
                    insertsys.Insert("简历子集", oldmodel.EndDate == null ? null : Convert.ToDateTime(oldmodel.EndDate).ToShortDateString(),
                        model.EndDate == null ? null : Convert.ToDateTime(model.EndDate).ToShortDateString(),
                        model.EmpBasicID, FangShi, "结束日期");
                if (oldmodel.Unit != model.Unit)
                    insertsys.Insert("简历子集", oldmodel.Unit, model.Unit,
                        model.EmpBasicID, FangShi, "所在单位");
                if (oldmodel.Duty != model.Duty)
                    insertsys.Insert("简历子集", oldmodel.Duty, model.Duty,
                        model.EmpBasicID, FangShi, "担任职务");

                if (oldmodel.Witness != model.Witness)
                    insertsys.Insert("简历子集", oldmodel.Witness, model.Witness,
                        model.EmpBasicID, FangShi, "证明人");
                if (oldmodel.IsPeriod != model.IsPeriod)
                    insertsys.Insert("简历子集", oldmodel.IsPeriod == false ? "否" : "是", model.IsPeriod == false ? "否" : "是",
                        model.EmpBasicID, FangShi, "是否为期间");
            }

        }
    }
}
